package net.rlair.esAnalyzeWord.util;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.util.ResourceBundle;

/**
 * elasticsearch 客户端.
 *
 * @author yao yi.
 * @version 1.0.0.0
 */

public final class EsClientUtil {

    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(EsClientUtil.class);

    /**
     * 屏蔽构造函数.
     */
    private EsClientUtil() {
    }

    /**
     * 集群名.
     */
    private static final String CLUSTER_NAME;

    /**
     * 用戶名:密码.
     */
    private static final String USER_PASSWORD;

    /**
     * 服务端口.
     */
    private static final String ELASTIC_PORT;

    /**
     * 服务器ip.
     */
    private static final String[] SERVER_HOSTS;

    /**
     * 公共的客户端.
     */
    private static TransportClient client;

    /**
     * 初始化常量参数.
     */
    static {
        final ResourceBundle elastic = ResourceBundle.getBundle("elastic");
        CLUSTER_NAME = elastic.getString("elastic.cluster");
        USER_PASSWORD = elastic.getString("elastic.user.password");
        ELASTIC_PORT = elastic.getString("elastic.tcp.port");
        final String ips = elastic.getString("elastic.server.hosts");
        SERVER_HOSTS = ips.split(",");
    }

    /**
     * 获取固定的client.
     * @return TransportClient.
     */
    public static TransportClient getClient(){
        if (client != null) {
            return client;
        }

        synchronized (TransportClient.class) {
            if (StringUtils.isBlank(CLUSTER_NAME) || StringUtils.isBlank(USER_PASSWORD)
                    || SERVER_HOSTS==null || SERVER_HOSTS.length==0) {
                LOGGER.error("Not set elastic configures");
                return null;
            }

            final Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME)
                    .put("xpack.security.user", USER_PASSWORD).build();

            try {
                client = new PreBuiltXPackTransportClient(settings);
                for (final String host : SERVER_HOSTS){
                    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),
                            Integer.valueOf(ELASTIC_PORT)));
                }
            }catch (final Exception e){
                LOGGER.error("ES client receive failed", e);
                return null;
            }
        }
        return client;
    }

    /**
     * 集群健康状况查询.
     * @return ClusterStatsResponse
     */
    public static ClusterStatsResponse getEsClusterInfo(){
        return getClient().admin().cluster().prepareClusterStats().execute().actionGet();
    }
}
